package com.talent.jiwen.my;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.http.result.FinanceBean;
import com.talent.jiwen.http.result.FinanceResult;
import com.talent.jiwen.util.DateUtils;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.Observable;


/**
 * Created by qyqx on 2018/9/5.
 * 账单明细
 */

public class RunningWaterActivity extends BaseActivity {

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @BindView(R.id.img_no_data)
    ImageView img_no_data;


    @Override
    protected int provideContentViewId() {
        return R.layout.activity_run_water;
    }

    @Override
    public String titleName() {
        return "账单明细";
    }

    @Override
    protected void initData() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        adapter=new CommonAdapter<FinanceBean>(mContext,R.layout.item_run_water,dataList) {
            @Override
            protected void convert(ViewHolder holder, FinanceBean financeBean, int position) {

                holder.setText(R.id.txt_time, DateUtils.ms2Date(financeBean.getCreateTime()));

                TextView spendTxt=holder.getView(R.id.txt_spend);//交易类别（1：教学消费 2：佣金提成 3：提现 4：教学收入）
                TextView spendType=holder.getView(R.id.txt_type);
//                String type=null;

                //1、收入 ；2、支出
                if(financeBean.getFinanceStatus() == 1){
                    spendTxt.setTextColor(getResources().getColor(R.color.common_app_color));
                    spendTxt.setText("+"+financeBean.getInvolveMoney());
                }else{
                    spendTxt.setTextColor(getResources().getColor(R.color.color_FFA421));
                    spendTxt.setText("-"+financeBean.getInvolveMoney());
                }
                spendType.setText(financeBean.getFinanceTypeName());
//                switch (financeBean.getFinanceType()){
//                    case 1:
//                        type = "教学消费";
//                        spendTxt.setTextColor(getResources().getColor(R.color.color_FFA421));
//                        spendTxt.setText("-"+financeBean.getInvolveMoney());
//                        break;
//                    case 2:
//                        type="佣金提成";
//                        spendTxt.setTextColor(getResources().getColor(R.color.common_app_color));
//                        spendTxt.setText("+"+financeBean.getInvolveMoney());
//                        break;
//                    case 3:
//                        type="提现";
//                        spendTxt.setTextColor(getResources().getColor(R.color.color_FFA421));
//                        spendTxt.setText("-"+financeBean.getInvolveMoney());
//                        break;
//                    case 4:
//                        type="教学收入";
//                        spendTxt.setTextColor(getResources().getColor(R.color.common_app_color));
//                        spendTxt.setText("+"+financeBean.getInvolveMoney());
//                        break;
//                    case 5:
//                        type="订单退款";
//                        spendTxt.setTextColor(getResources().getColor(R.color.common_app_color));
//                        spendTxt.setText("+"+financeBean.getInvolveMoney());
//                        break;
//                    case 6:
//                        type="支付保证金";
//                        spendTxt.setTextColor(getResources().getColor(R.color.color_FFA421));
//                        spendTxt.setText("-"+financeBean.getInvolveMoney());
//                        break;
//                    case 7:
//                        type="教师资金冻结";
//                        spendTxt.setTextColor(getResources().getColor(R.color.color_FFA421));
//                        spendTxt.setText("-"+financeBean.getInvolveMoney());
//                        break;
//                    case 8:
//                        type="首单奖励金";
//                        spendTxt.setTextColor(getResources().getColor(R.color.common_app_color));
//                        spendTxt.setText("+"+financeBean.getInvolveMoney());
//                        break;
//                    case 9:
//                        type="冻结资金返回";
//                        spendTxt.setTextColor(getResources().getColor(R.color.common_app_color));
//                        spendTxt.setText("+"+financeBean.getInvolveMoney());
//                        break;
//
//                }
//                spendType.setText(type);

            }
        };
        recyclerView.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                startIndex = 1;
                getRunningWaterList();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                startIndex ++;
                getRunningWaterList();
            }
        });

        refreshLayout.autoRefresh();
    }


    CommonAdapter<FinanceBean> adapter;


    private int startIndex = 1;
    private List<FinanceBean> dataList = new ArrayList<>();

    /**获取资金明细
     *
     */
    private void getRunningWaterList(){
        Map<String, String> map = new HashMap<>();
        map.put("studentId" , SPUtil.getUserId());
        map.put("startIndex" , startIndex + "");

        Observable observable = ApiFactory.getEducationApiSingleton().getFinanceChangeLog(SPUtil.stringOut(SPConstant.TOKEN) , map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<FinanceResult>(this) {
            @Override
            protected void _onNext(FinanceResult result) {
                if(startIndex == 1){
                    dataList.clear();
                    dataList.addAll(result.getChangeLogList());
                    adapter.notifyDataSetChanged();
                    refreshLayout.finishRefresh();
                }else{
                    dataList.addAll(result.getChangeLogList());
                    adapter.notifyDataSetChanged();
                    refreshLayout.finishLoadMore();

                    if (result.getChangeLogList().size() < 10){
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }

                if (Validators.isEmpty(dataList)){
                    if(img_no_data!=null){
                        img_no_data.setVisibility(View.VISIBLE);
                    }

                }else{
                    if(img_no_data!=null){
                        img_no_data.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
                refreshLayout.finishRefresh();
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,false);
    }
}
